﻿/*
 * Copyright (c) 2015-2016 Constantijn Evenhuis (blueprints@const.nl)
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
using Blueprints.Resolution;
using System;
using System.Reflection;

namespace Blueprints.Defaults
{
    /// <summary>
    /// A read-only object property.
    /// </summary>
    internal sealed class ReadOnlyObjectProperty : IPropertyDescriptor
    {
        private readonly PropertyInfo propertyInfo;
        private readonly IObjectDescriptor descriptor;

        /// <summary>
        /// Creates a new PropertyInfoDescriptor.
        /// </summary>
        /// <param name="propertyInfo">The property to describe.</param>
        /// <param name="descriptor">The object descriptor of the property type.</param>
        public ReadOnlyObjectProperty(PropertyInfo propertyInfo, IObjectDescriptor descriptor)
        {
            this.descriptor = descriptor;
            this.propertyInfo = propertyInfo;
            Name = this.propertyInfo.Name;
            Type = typeof(PropertyBucket<>).MakeGenericType(propertyInfo.PropertyType);
        }

        /// <summary>
        /// Gets the name of the property.
        /// </summary>
        public string Name
        {
            get;
            private set;
        }

        /// <summary>
        /// Gets the type of property.
        /// </summary>
        public Type Type
        {
            get;
            private set;
        }

        /// <summary>
        /// Gets the value of the property for <paramref name="instance"/>.
        /// </summary>
        /// <param name="instance">The instance which contains the property.</param>
        /// <returns>The value of the property.</returns>
        public object GetValue(object instance)
        {
            object propertyValue = propertyInfo.GetValue(instance);
            return Activator.CreateInstance(Type, new object[] { descriptor, propertyValue });
        }

        /// <summary>
        /// Sets the value of the property for <paramref name="instance"/>.
        /// </summary>
        /// <param name="instance">The instance which contains the property.</param>
        /// <param name="value">The value to set.</param>
        public void SetValue(object instance, object value)
        {
            PropertyBucket bucket = (PropertyBucket)value;
            object propertyValue = propertyInfo.GetValue(instance);
            if (bucket == null)
            {
                throw new BlueprintException("Error setting values to the read-only property: property is null");
            }

            bucket.ApplyTo(propertyValue);
        }

        /// <summary>
        /// Indicates whether <paramref name="value"/> should be serialized for <paramref name="instance"/>. Omitting
        /// default values yields smaller files. 
        /// </summary>
        /// <param name="instance">The instance which contains the property.</param>
        /// <param name="value">The property value.</param>
        /// <returns>True if the value should be serialized, false otherwise.</returns>
        public bool ShouldSerializeValue(object instance, object value)
        {
            PropertyBucket bucket = (PropertyBucket)value;
            return bucket.ShouldSerializeValue();
        }
    }
}